package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.data.ResponseStausEnum;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.*;
import com.dongdongshop.util.PageBean;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebParam;
import java.util.List;

/**
 * @Description
 * @ClassName GoodController
 * @Author RC
 * @date 2020.12.13 15:11
 */
@Controller
@RequestMapping("good")
public class GoodController {
    @Reference(timeout = 1000000,check = false)
    private GoodService goodService;
    @Reference(timeout = 100000,check = false)
    private ItemCatService itemCatService;
    @Reference(timeout = 100000,check = false)
    private TempLateService tempLateService;
    @Reference(timeout = 1000000,check = false)
    private GoodsDescService goodsDescService;
    @Reference(timeout = 1000000,check = false)
    private SpecificationOptionService specificationOptionService;
    private static final Logger logger = LoggerFactory.getLogger(SellerController.class);


    @RequestMapping("getItemCat")
    @ResponseBody
    public DataResult  getItemCatList (Long parentId){
      try{
          logger.info("进入商品分类查询方法");
          List<TbItemCat> itemCats = itemCatService.getItemCatList(parentId);

          return DataResult.response(ResponseStausEnum.SUSSCESS).setData(itemCats);
      }catch (Exception e){
          logger.info("查询失败",e);
          return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
      }

    }
    @RequestMapping("getBrandByTempId")
    @ResponseBody
    public DataResult  getTemplate (Long id){
        try {
            logger.info("进入品牌查询方法");
            //根据分类id查品牌
            long typeId = itemCatService.getBrandByTempId(id);
            TbTypeTemplate template = tempLateService.getBrandByItemId(typeId);

            return DataResult.response(ResponseStausEnum.SUSSCESS).setData(template);
        }catch (Exception e){
            logger.info("查询失败",e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }

    }

    @RequestMapping("saveGoods")
    @ResponseBody
    public DataResult  saveGoods (TbGoods tbGoods, TbGoodsDesc tbGoodsDesc,String str){
        try {
            //获取商家id
            Subject subject = SecurityUtils.getSubject();
            TbSeller subject1 =(TbSeller)  subject.getPrincipal();
            tbGoods.setSellerId(subject1.getSellerId());
            List<TbItem> tbItems = JSON.parseArray(str, TbItem.class);
            logger.info("进入保存商品方法");
           goodService.saveGoods(tbGoods,tbGoodsDesc,tbItems);
            return DataResult.response(ResponseStausEnum.SUSSCESS);
        }catch (Exception e){
            logger.info("查询失败",e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }

    }

    //查询规格展示
    @RequestMapping("getSpecOptions")
    @ResponseBody
    public DataResult getSpecsAndOptionsByItemCatId(Long id){
        try{
            logger.info("进入规格查询方法");
            //查看item_cat表中的type_id
            long typeId = itemCatService.getBrandByTempId(id);
            //根据itemCat中的模板Id进行查询规格  使用tb_item_cat中的type_id来进行查询tb_type_template表中字段
            TbTypeTemplate template = tempLateService.getBrandByItemId(typeId);
            //把查出来的SpecIds转成list
            List<SpecVo> specVoList = JSON.parseArray(template.getSpecIds(),SpecVo.class);
            for (SpecVo specVo : specVoList){
                //根据specVo中的id查询商品具体规格
                List<TbSpecificationOption> tbSpecificationOptionsById = specificationOptionService.getInfoById(specVo.getId());
                specVo.setOptions(tbSpecificationOptionsById);
            }
            logger.info("查询规格");
            return DataResult.response(ResponseStausEnum.SUSSCESS).setData(specVoList);
        }catch (Exception e){
            logger.error("查询规格失败",e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }
    }

    //商家平台展示商品
    @RequestMapping("getGoodsList")
    @ResponseBody
    public DataResult getGoodsList(@RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "5") Integer pageSize, TbGoods tbGoods) {
        try {
            Subject subject = SecurityUtils.getSubject();
            TbSeller subject1 =(TbSeller)  subject.getPrincipal();
            tbGoods.setSellerId(subject1.getSellerId());
            logger.info("进入商品审核方法");
            PageBean<GoodsVO> tbGoods1 = goodService.getGoodsList(pageNumber, pageSize, tbGoods);
            return DataResult.response(ResponseStausEnum.SUSSCESS).setData(tbGoods1);

        } catch (Exception e) {
            logger.info("查询失败", e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }
    }
    //提交审核
    @RequestMapping("submitApproved")
    @ResponseBody
    public DataResult submitApproved(@RequestParam("ids[]") Long[] ids) {
        try {
            logger.info("进入商品审核方法");
            goodService.updateStatusByIds(ids);
            return DataResult.response(ResponseStausEnum.SUSSCESS);

        } catch (Exception e) {
            logger.info("查询失败", e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }
    }
    //是否删除
    @RequestMapping("deleteBath")
    @ResponseBody
    public DataResult deleteBath(@RequestParam("ids[]")Long[] ids){
        try{
            logger.info("删除");
            goodService.deleteBath(ids);

            return DataResult.response(ResponseStausEnum.SUSSCESS);
        }catch (Exception e){
            logger.error("审核失败",e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }
    }

}
